﻿using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using FarmSmartSleep.Organ.API.Application.RaiseCommands;
using FarmSmartSleep.Organ.API.Application.SleepInfoCommands.FemaleBeginCommands;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Descendantsafter
{
    public class DescendantsafterHandler : IRequestHandler<DescendantsafterCommand, Result>
    {
        private readonly IBaseRepository<Descendants> descendantsRepository;//后裔转钟
        private readonly ILogger<DescendantsafterCommand> logger;//日志

        public DescendantsafterHandler(IBaseRepository<Descendants> descendantsRepository, ILogger<DescendantsafterCommand> logger)
        {
            this.descendantsRepository = descendantsRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 批量添加后裔转种信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(DescendantsafterCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result() { Code = (int)CodeEnum.success };
                var data = await descendantsRepository.AddRange(request.Datas);
                result.Code = (int)CodeEnum.success;
                result.Data = data;
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("批量添加后裔转种信息方法调用失败" + ex);
                throw;
            }
        }
    }
}
